/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.staff;

import entity.StakeholderEntity;
import exception.ExistException;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import managedbean.PageRedirectorBean;
import org.primefaces.model.MenuModel;
import session.stateless.StaffAccountSessionBean;
import session.stateless.StakeholderSessionBeanStateless;
import util.entityhelper.StaffAccountState;
import util.generator.StaffMenuGenerator;
import util.localization.WebLocalization;

/**
 *
 * @author WenYong
 */
@ManagedBean
@RequestScoped
public class StaffVerifyStakeholderAcctManagedBean {

    // fields for this bean
    private FacesContext ctx = FacesContext.getCurrentInstance();
    private WebLocalization localization;
    // fields passed from session
    private Long staffAccountId;
    private String preferredLocale;
    private MenuModel staffMenuModel;
    private List<StakeholderEntity> accountList;
    private PageRedirectorBean redirect;
    @EJB
    private StaffAccountSessionBean staffAccountMgr;
    // use entity helpers to extract data (attribute extraction and formatting instead of entity in this open bean)
    private StaffAccountState staffAcctState;
    @EJB
    StakeholderSessionBeanStateless sessionBean;

    /** Creates a new instance of StaffVerifyStakeholderAcctManagedBean */
    public StaffVerifyStakeholderAcctManagedBean() {
        /* LOCALIZATION */
        localization = new WebLocalization();
        preferredLocale = localization.getPreferredLocaleFromSession();
        /* END LOCALIZATION */
  
        /* pre-load values from session and database */
        // get account ID values from session
        if (ctx.getExternalContext().getSessionMap().containsKey("staffAccountId")) {
            // get session values
            staffAccountId = (Long) ctx.getExternalContext().getSessionMap().get("staffAccountId");
            // put back session values
            ctx.getExternalContext().getSessionMap().put("staffAccountId", staffAccountId);
        } else {
            // redirect back to login
            redirect = new PageRedirectorBean();
            redirect.goStaffLoginPageAtPreviousLevel();
        }
        /* END load values from session and database */
    }

    public void initView(PhaseEvent event) {
        try {
            if(accountList == null)
            {
                              accountList = sessionBean.getAllStakeholders(); 
            }
            // set language for all MANAGERS for all THROWABLE EXCEPTIONS in backend
            // to use this line everytime possible language changes
            staffAccountMgr.setSelectedLocale(getPreferredLocale());
            // get data from backend, exception alrd thrown by backend if these methods fail.

            staffAcctState = staffAccountMgr.getStaffAccountByID(staffAccountId);

            /* PRE-SET MENU BASED ON ACCESS RIGHTS */
            StaffMenuGenerator menuGenerator = new StaffMenuGenerator(staffAcctState);
            setStaffMenuModel(menuGenerator.generateStaffMenu());
            /* END PRE-SET MENU BASED ON ACCESS RIGHTS */

        } catch (ExistException ex) {
//            System.out.println(ex.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getMessage(), ""));
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
//            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getMessage(), ""));
        }
    }

    /**
     * @return the staffMenuModel
     */
    public MenuModel getStaffMenuModel() {
        return staffMenuModel;
    }

    /**
     * @param staffMenuModel the staffMenuModel to set
     */
    public void setStaffMenuModel(MenuModel staffMenuModel) {
        this.staffMenuModel = staffMenuModel;
    }

    /**
     * @return the accountList
     */
    public List<StakeholderEntity> getAccountList() {
        return accountList;
    }

    /**
     * @param accountList the accountList to set
     */
    public void setAccountList(List<StakeholderEntity> accountList) {
        this.accountList = accountList;
    }

    /**
     * @return the preferredLocale
     */
    public String getPreferredLocale() {
        return preferredLocale;
    }

    /**
     * @param preferredLocale the preferredLocale to set
     */
    public void setPreferredLocale(String preferredLocale) {
        this.preferredLocale = preferredLocale;
    }
    
     public void confirmChanges (ActionEvent event)
    {
        for(int i=0; i<getAccountList().size(); i++)
        {
            staffAccountMgr.verifyAcc(getAccountList().get(i));
        }
    }
}
